{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-06-12T01:33:02.550227Z",
     "start_time": "2025-06-12T01:32:02.818762Z"
    }
   },
   "source": [
    "import jieba\n",
    "import numpy as np\n",
    "\n",
    "# 1. 读取停用词表\n",
    "with open(r'E:\\Ruanjian\\Python\\pycharm\\Ren\\Ren\\机器学习\\THUCNews\\stopwords-zh(1).txt', encoding='utf-8') as f:\n",
    "    stopwords = set([line.strip() for line in f])\n",
    "\n",
    "# 2. 读取数据\n",
    "def load_data(filename):\n",
    "    texts, labels = [], []\n",
    "    with open(filename, encoding='utf-8') as f:\n",
    "        for line in f:\n",
    "            parts = line.strip().split('\\t')\n",
    "            if len(parts) == 2:\n",
    "                text, label = parts\n",
    "                texts.append(text)\n",
    "                labels.append(int(label))\n",
    "    return texts, np.array(labels)\n",
    "\n",
    "train_texts, train_labels = load_data(r'E:\\Ruanjian\\Python\\pycharm\\Ren\\Ren\\机器学习\\THUCNews\\train.txt')\n",
    "test_texts, test_labels = load_data(r'E:\\Ruanjian\\Python\\pycharm\\Ren\\Ren\\机器学习\\THUCNews\\test.txt')\n",
    "\n",
    "# 3. 分词和去停用词\n",
    "def preprocess(texts):\n",
    "    result = []\n",
    "    for text in texts:\n",
    "        words = [w for w in jieba.lcut(text) if w not in stopwords and w.strip()]\n",
    "        result.append(words)\n",
    "    return result\n",
    "\n",
    "train_words = preprocess(train_texts)\n",
    "test_words = preprocess(test_texts)\n",
    "\n",
    "# 4. 构建词表\n",
    "vocab = {}\n",
    "for words in train_words:\n",
    "    for w in words:\n",
    "        if w not in vocab:\n",
    "            vocab[w] = len(vocab)\n",
    "vocab_size = len(vocab)\n",
    "\n",
    "# 5. 向量化\n",
    "from scipy.sparse import lil_matrix\n",
    "\n",
    "def vectorize(words_list):\n",
    "    X = lil_matrix((len(words_list), vocab_size))\n",
    "    for i, words in enumerate(words_list):\n",
    "        for w in words:\n",
    "            if w in vocab:\n",
    "                X[i, vocab[w]] += 1\n",
    "    return X.tocsr()\n",
    "\n",
    "X_train = vectorize(train_words)\n",
    "X_test = vectorize(test_words)\n",
    "print(f\"处理后词表大小（不同词的数量）: {vocab_size}\")"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Dumping model to file cache C:\\Users\\任老大\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 2.228 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "处理后词表大小（不同词的数量）: 117949\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-12T01:34:56.823774Z",
     "start_time": "2025-06-12T01:34:11.114871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.svm import LinearSVC\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_squared_error\n",
    "import time\n",
    "\n",
    "# 训练支持向量机并计时\n",
    "start_time = time.time()\n",
    "clf = LinearSVC()\n",
    "clf.fit(X_train, train_labels)\n",
    "train_time = time.time() - start_time\n",
    "\n",
    "# 预测\n",
    "preds = clf.predict(X_test)\n",
    "\n",
    "# 评估指标\n",
    "acc = accuracy_score(test_labels, preds)\n",
    "prec = precision_score(test_labels, preds, average='macro')\n",
    "rec = recall_score(test_labels, preds, average='macro')\n",
    "f1 = f1_score(test_labels, preds, average='macro')\n",
    "mse = mean_squared_error(test_labels, preds)\n",
    "\n",
    "print(f'准确率: {acc:.4f}')\n",
    "print(f'精确率: {prec:.4f}')\n",
    "print(f'均方误差: {mse:.4f}')\n",
    "print(f'召回率: {rec:.4f}')\n",
    "print(f'F1分数: {f1:.4f}')\n",
    "print(f'训练时间: {train_time:.2f} 秒')"
   ],
   "id": "78bddc57f94ee1d8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 0.9012\n",
      "精确率: 0.9019\n",
      "均方误差: 1.2098\n",
      "召回率: 0.9012\n",
      "F1分数: 0.9014\n",
      "训练时间: 44.64 秒\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-12T01:37:27.392098Z",
     "start_time": "2025-06-12T01:36:14.183896Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_squared_error\n",
    "import time\n",
    "\n",
    "# 训练逻辑回归模型并计时\n",
    "start_time = time.time()\n",
    "clf = LogisticRegression(max_iter=1000)\n",
    "clf.fit(X_train, train_labels)\n",
    "train_time = time.time() - start_time\n",
    "\n",
    "# 预测\n",
    "preds = clf.predict(X_test)\n",
    "\n",
    "# 评估指标\n",
    "acc = accuracy_score(test_labels, preds)\n",
    "prec = precision_score(test_labels, preds, average='macro')\n",
    "rec = recall_score(test_labels, preds, average='macro')\n",
    "f1 = f1_score(test_labels, preds, average='macro')\n",
    "mse = mean_squared_error(test_labels, preds)\n",
    "\n",
    "print(f'准确率: {acc:.4f}')\n",
    "print(f'精确率: {prec:.4f}')\n",
    "print(f'均方误差: {mse:.4f}')\n",
    "print(f'召回率: {rec:.4f}')\n",
    "print(f'F1分数: {f1:.4f}')\n",
    "print(f'训练时间: {train_time:.2f} 秒')"
   ],
   "id": "f6f7392b79e82888",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 0.9077\n",
      "精确率: 0.9087\n",
      "均方误差: 1.0972\n",
      "召回率: 0.9077\n",
      "F1分数: 0.9080\n",
      "训练时间: 73.16 秒\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-12T01:48:42.932965Z",
     "start_time": "2025-06-12T01:37:27.637381Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_squared_error\n",
    "import time\n",
    "\n",
    "# 训练决策树模型并计时\n",
    "start_time = time.time()\n",
    "clf = DecisionTreeClassifier()\n",
    "clf.fit(X_train, train_labels)\n",
    "train_time = time.time() - start_time\n",
    "\n",
    "# 预测\n",
    "preds = clf.predict(X_test)\n",
    "\n",
    "# 评估指标\n",
    "acc = accuracy_score(test_labels, preds)\n",
    "prec = precision_score(test_labels, preds, average='macro')\n",
    "rec = recall_score(test_labels, preds, average='macro')\n",
    "f1 = f1_score(test_labels, preds, average='macro')\n",
    "mse = mean_squared_error(test_labels, preds)\n",
    "\n",
    "print(f'准确率: {acc:.4f}')\n",
    "print(f'精确率: {prec:.4f}')\n",
    "print(f'均方误差: {mse:.4f}')\n",
    "print(f'召回率: {rec:.4f}')\n",
    "print(f'F1分数: {f1:.4f}')\n",
    "print(f'训练时间: {train_time:.2f} 秒')"
   ],
   "id": "b0365dd12eee0838",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 0.7795\n",
      "精确率: 0.7833\n",
      "均方误差: 3.1504\n",
      "召回率: 0.7795\n",
      "F1分数: 0.7807\n",
      "训练时间: 674.98 秒\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-12T02:59:09.834958Z",
     "start_time": "2025-06-12T01:48:43.076395Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_squared_error\n",
    "import time\n",
    "\n",
    "# 训练随机森林模型并计时\n",
    "start_time = time.time()\n",
    "clf = RandomForestClassifier(n_estimators=100, random_state=42)\n",
    "clf.fit(X_train, train_labels)\n",
    "train_time = time.time() - start_time\n",
    "\n",
    "# 预测\n",
    "preds = clf.predict(X_test)\n",
    "\n",
    "# 评估指标\n",
    "acc = accuracy_score(test_labels, preds)\n",
    "prec = precision_score(test_labels, preds, average='macro')\n",
    "rec = recall_score(test_labels, preds, average='macro')\n",
    "f1 = f1_score(test_labels, preds, average='macro')\n",
    "mse = mean_squared_error(test_labels, preds)\n",
    "\n",
    "print(f'准确率: {acc:.4f}')\n",
    "print(f'精确率: {prec:.4f}')\n",
    "print(f'均方误差: {mse:.4f}')\n",
    "print(f'召回率: {rec:.4f}')\n",
    "print(f'F1分数: {f1:.4f}')\n",
    "print(f'训练时间: {train_time:.2f} 秒')"
   ],
   "id": "6cfbfc8bc9147d06",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "KeyboardInterrupt\n",
      "\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-12T03:00:39.125943Z",
     "start_time": "2025-06-12T02:59:16.914270Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_squared_error\n",
    "import time\n",
    "\n",
    "# 训练K近邻模型并计时\n",
    "start_time = time.time()\n",
    "clf = KNeighborsClassifier(n_neighbors=3, metric='cosine')  # 余弦距离+邻居数3\n",
    "clf.fit(X_train, train_labels)\n",
    "train_time = time.time() - start_time\n",
    "\n",
    "# 预测\n",
    "preds = clf.predict(X_test)\n",
    "\n",
    "# 评估指标\n",
    "acc = accuracy_score(test_labels, preds)\n",
    "prec = precision_score(test_labels, preds, average='macro')\n",
    "rec = recall_score(test_labels, preds, average='macro')\n",
    "f1 = f1_score(test_labels, preds, average='macro')\n",
    "mse = mean_squared_error(test_labels, preds)\n",
    "\n",
    "print(f'准确率: {acc:.4f}')\n",
    "print(f'精确率: {prec:.4f}')\n",
    "print(f'均方误差: {mse:.4f}')\n",
    "print(f'召回率: {rec:.4f}')\n",
    "print(f'F1分数: {f1:.4f}')\n",
    "print(f'训练时间: {train_time:.2f} 秒')"
   ],
   "id": "e32f78a2c8aef191",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 0.7790\n",
      "精确率: 0.7922\n",
      "均方误差: 3.9009\n",
      "召回率: 0.7790\n",
      "F1分数: 0.7786\n",
      "训练时间: 0.13 秒\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-12T03:00:44.975066Z",
     "start_time": "2025-06-12T03:00:44.615555Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, mean_squared_error\n",
    "import time\n",
    "\n",
    "# 训练朴素贝叶斯模型并计时\n",
    "start_time = time.time()\n",
    "clf = MultinomialNB()\n",
    "clf.fit(X_train, train_labels)\n",
    "train_time = time.time() - start_time\n",
    "\n",
    "# 预测\n",
    "preds = clf.predict(X_test)\n",
    "\n",
    "# 评估指标\n",
    "acc = accuracy_score(test_labels, preds)\n",
    "prec = precision_score(test_labels, preds, average='macro')\n",
    "rec = recall_score(test_labels, preds, average='macro')\n",
    "f1 = f1_score(test_labels, preds, average='macro')\n",
    "mse = mean_squared_error(test_labels, preds)\n",
    "\n",
    "print(f'准确率: {acc:.4f}')\n",
    "print(f'精确率: {prec:.4f}')\n",
    "print(f'均方误差: {mse:.4f}')\n",
    "print(f'召回率: {rec:.4f}')\n",
    "print(f'F1分数: {f1:.4f}')\n",
    "print(f'训练时间: {train_time:.2f} 秒')"
   ],
   "id": "bda3de48a1b373a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率: 0.8917\n",
      "精确率: 0.8932\n",
      "均方误差: 1.3468\n",
      "召回率: 0.8917\n",
      "F1分数: 0.8916\n",
      "训练时间: 0.26 秒\n"
     ]
    }
   ],
   "execution_count": 10
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
